package todo;


import done.AbstractWashingMachine;
import se.lth.cs.realtime.*;


public class SpinController extends PeriodicThread {
    // TODO: add suitable attributes
    
	boolean spinn;
	AbstractWashingMachine theMachine;
	int mode;
	int timer;
	int periodForSpin;

    public SpinController(AbstractWashingMachine theMachine, long speed) {
	super(speed); // TODO: replace with suitable period
	spinn = true;
	this.theMachine = theMachine;
	mode=0;
	timer=0;
	periodForSpin = 60;
    }

    public void perform() {
    	SpinEvent e = (SpinEvent) mailbox.tryFetch();
    	if(e!= null){
    		mode = e.getMode();
    		
    		e=null;
    	}
    	
    	
    	if(mode == SpinEvent.SPIN_OFF){
    		theMachine.setSpin(SpinEvent.SPIN_OFF);
    	}else if(mode == SpinEvent.SPIN_SLOW){
    		timer++;
    		
    		if(timer == periodForSpin){
    			spinn = !spinn;
    			timer =0;
    		}
        	if(spinn){
        		theMachine.setSpin(theMachine.SPIN_LEFT);
        	}else{
        		theMachine.setSpin(theMachine.SPIN_RIGHT);
        	}
    		
        	
        	
    	}else if(mode == 2){
    		theMachine.setSpin(SpinEvent.SPIN_FAST);
    	}
    	

    	
    	
    	
    	
    }
}
